package com.mornd.gulimall.ware.controller;

import com.mornd.gulimall.common.core.result.AjaxResult;
import com.mornd.gulimall.common.core.validation.UpdateGroup;
import com.mornd.gulimall.ware.entity.Purchase;
import com.mornd.gulimall.ware.service.IPurchaseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

import static com.mornd.gulimall.common.core.result.AjaxResult.success;

/**
 * <p>
 * 采购单信息 前端控制器
 * </p>
 *
 * @author mornd
 * @since 2023-07-29
 */

@Validated
@RestController
@RequestMapping("/ware/purchase")
public class PurchaseController {

    @Resource
    private IPurchaseService purchaseService;

    @GetMapping
    public AjaxResult list(Purchase purchase) {
        return success(purchaseService.list(purchase));
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(purchaseService.getById(id));
    }

    @PutMapping
    public AjaxResult save(@RequestBody @Validated Purchase purchase) {
        purchaseService.save(purchase);
        return success();
    }

    @PostMapping
    public AjaxResult update(@RequestBody @Validated(UpdateGroup.class) Purchase purchase) {
        purchaseService.updateById(purchase);
        return success();
    }

    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));
        return success();
    }

    /**
     * 获取未接收的采购单列表
     * @return
     */
    @GetMapping("/unreceive")
    public AjaxResult unreceive() {
        return success(purchaseService.unreceive());
    }

    /**
     * 采购人员领取采购单
     * @param ids 采购单ids
     * @return
     */
    @PostMapping("/received")
    public AjaxResult received(@RequestBody @NotNull @NotEmpty Long[] ids) {
        // todo
        return success();
    }

    /**
     * 完成采购单
     * @return
     */
    @PostMapping("/done")
    public AjaxResult done() {
        // todo
        return success();
    }

}
